<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #areaDiv {
        width: 200px;
        height: 50px;
        border: 1px solid black;
      }
      #showMsg {
        width: 200px;
        height: 20px;
        border: 1px solid black;
        margin-top: 10px;
      }
    </style>
    <script>
      window.onload = function () {
        /**
         * 当鼠标在areaDiv中移动时，在showMsg来显示鼠标的坐标
         *
         */
        var areaDiv = document.getElementById("areaDiv");
        var showMsg = document.getElementById("showMsg");
        areaDiv.onmousemove = function (event) {
          // 可以获取鼠标指针的水平坐标
          var x = event.clientX;
          // 可以获取鼠标指针的垂直坐标
          var y = event.clientY;
          console.log(event);

          showMsg.innerHTML = "x=" + x + ",y=" + y;
        };
      };
    </script>
  </head>
  <body>
    <div id="areaDiv"></div>
    <div id="showMsg"></div>
  </body>
</html>
